package cn.huanglei936.essay.io;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

@Slf4j
public class CopyFile {

    @Test
    public void test() throws IOException {
        String src = "E:\\disk\\告白气球.mp4";
        String dest = "E:\\disk\\mp3\\告白气球.mp4";
        FileUtils.copyFile(new File(src), new File(dest));
    }

    @Test
    public void copy() {
        String srcPath = "E:\\disk\\mp3";
        File fileSrcPath = new File(srcPath);
        String destPath = srcPath + "Ⅱ";
        File fileDestPath = new File(destPath);
        if (!fileDestPath.exists()) {
            fileDestPath.mkdirs();
        }
        String[] list = fileSrcPath.list();
        for (String name : list) {
            log.info("name = {}", name);
        }
    }

    /**
     * 重命名
     *
     * @throws IOException
     */
    @Test
    public void rename() throws IOException {
        String targetStr = "周杰伦";
        String dirStr = "C:\\disk\\mp3";
        Path dir = Paths.get(dirStr);

        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String oldFileName = file.toString();
                if (oldFileName.indexOf(targetStr) > -1) {
                    String newFileName = oldFileName.replace(targetStr, "");
                    Path newFile = Paths.get(newFileName);
                    Files.move(file, newFile);

                    log.info("newFileName: {}", newFileName);
                }
                else {
                    log.info("符合规则: {}", oldFileName);
                }
                return super.visitFile(file, attrs);
            }
        });
    }

    /**
     * 拷贝数据
     *
     * @throws IOException
     */
    @Test
    public void pathCopy() throws IOException {

        String srcPath = "C:\\disk\\mp3";

        Path pathSrcPath = Paths.get(srcPath);

        String targetPath = srcPath + "Ⅱ";
        Path targetPathPath = Paths.get(targetPath);

        // 判断文件是否存在
        if (!Files.exists(targetPathPath)) {
            Path directories = Files.createDirectories(targetPathPath);
            log.info("创建目录", directories.toString());
        }
        else {
            log.info("目录已存在", targetPathPath.toString());
        }
        Files.walkFileTree(pathSrcPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                log.info("file= {}", file.toString());
                String targetFileStr = targetPath + File.separator + file.getFileName().toString();
                Path targetFile = Paths.get(targetFileStr);
                if (!Files.exists(targetFile)) {
                    Files.copy(file, Paths.get(targetFileStr));
                }
                log.info("success：{}", targetFile.getFileName().toString());
                return super.visitFile(file, attrs);
            }
        });
    }

}
